package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Product;
import com.itheima.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /*
    查询所有产品信息
     */
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(ModelAndView mv , @RequestParam(name = "page", required = true,defaultValue = "1") int page,@RequestParam(name = "size" ,required = true ,defaultValue = "5") int size){
        List<Product> productList = productService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(productList);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("product-page-list");
        return mv;
    }

    /*
    添加数据
     */
    @RequestMapping("/save.do")
    public String save(Product product){
        productService.save(product);
        return "redirect:/product/findAll.do";
    }
}
